//
//  BootViewController.m
//  RL_项目二
//
//  Created by rimi on 16/3/15.
//  Copyright © 2016年 ruifengpeixun. All rights reserved.
//

#import "BootViewController.h"
#import "ViewController.h"
#import "AppDelegate.h"
#import "FitOfScreen.h"
#import <CoreLocation/CoreLocation.h>

#define SCREEN_WIDTH self.view.frame.size.width
#define SCREEN_HEIGHT self.view.frame.size.height

@interface BootViewController ()<UIScrollViewDelegate>
@property (nonatomic , strong) NSString *cityName;
@property (nonatomic , strong) UIScrollView *bootView;
@property (nonatomic , strong) ViewController *mianVC;
@property (nonatomic , assign) BOOL isOut;
@property (nonatomic , assign) BOOL isFirst;
@property (nonatomic , strong) UIImageView *image;

@end

@implementation BootViewController

- (UIImageView *)image{
    if (!_image) {
        _image = [[UIImageView alloc]init];
    }
    return _image;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.9412 green:0.9412 blue:0.9412 alpha:1.0];
    self.image.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    _mianVC = [[ViewController alloc]init];
    _isFirst  = [[NSUserDefaults standardUserDefaults]boolForKey:@"isFirstLoad"];
    _cityName = [[NSUserDefaults standardUserDefaults]objectForKey:@"city"];
    if (!_cityName) {
        _cityName = @"北京";
    }
    [self BootVC];

}

#pragma mark scrollView的代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"%f",scrollView.contentOffset.x);
      
        if (scrollView.contentOffset.x > 2*SCREEN_WIDTH + 20) {
            
            _isOut=YES;
            
        }
}
//停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
        if (_isOut) {
            
            [UIView animateWithDuration:0.5 animations:^{
                                                                                                                                                            
                _bootView.alpha = 0;
                [self Welcome];
                
            }completion:^(BOOL finished) {
               
                _mianVC.city = _cityName;
                [self performSelector:@selector(bootVC) withObject:nil afterDelay:2];

            } ];
        }
}


- (void)bootVC {
    [AppDelegate shareInstance].window.rootViewController = _mianVC;


}

- (void)BootVC {
    if (!_isFirst) {
        
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isFirstLoad"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        [self Component];
        
    }else{
         _mianVC.city = _cityName;
         [self Welcome];
         [self performSelector:@selector(bootVC) withObject:nil afterDelay:2];
    }
}
- (void)Component {
    
    _bootView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _bootView.backgroundColor = [UIColor colorWithRed:0.9412 green:0.9412 blue:0.9412 alpha:1.0];
    _bootView.contentSize = CGSizeMake(SCREEN_WIDTH * 3 + 30, SCREEN_HEIGHT);
    _bootView.pagingEnabled = YES;
    _bootView.bounces = NO;
    _bootView.showsHorizontalScrollIndicator = NO;
    _bootView.delegate = self;
    [self.view addSubview:_bootView];
    
    for (int i = 0 ; i < 3 ; i ++) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake( i * LRLCurrentScreenWidth,0,LRLCurrentScreenWidth , LRLCurrentScreenHeight)];
        image.image = [UIImage imageNamed:[NSString stringWithFormat:@"huanying%d",i+1]];
        [_bootView addSubview:image];
        
    }
    
}
//欢迎页面方法
- (void)Welcome{
    self.image.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    self.image.alpha = 0;
    NSString *date = [[NSString alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"h"];
    date = [formatter stringFromDate:[NSDate date]];
    NSInteger newdate = [date integerValue];
    NSLog(@"11111111111111111%ld",newdate);
    switch (newdate) {
        case 1:
            self.image.image = [UIImage imageNamed:@"һ"];
            break;
        case 2:
            self.image.image = [UIImage imageNamed:@"ʮ"];
            break;
        case 3:
            self.image.image = [UIImage imageNamed:@"Q"];
            break;
        case 4:
            self.image.image = [UIImage imageNamed:@"J"];
            break;
        case 5:
            self.image.image = [UIImage imageNamed:@"K"];
            break;
        case 6:
            self.image.image = [UIImage imageNamed:@"二"];
            break;
        case 7:
            self.image.image = [UIImage imageNamed:@"四"];
            break;
        case 8:
            self.image.image = [UIImage imageNamed:@"五"];
            break;
        case 9:
            self.image.image = [UIImage imageNamed:@"七"];
            break;
        case 10:
            self.image.image = [UIImage imageNamed:@"九"];
            break;
        case 11:
            self.image.image = [UIImage imageNamed:@"三"];
            break;
        case 12:
            self.image.image = [UIImage imageNamed:@"六"];
            break;
        default:
            self.image.image = [UIImage imageNamed:@"六"];
            break;
    }
    
    [self.view addSubview:self.image];
    
    [UIView animateWithDuration:1 animations:^{
        self.image.alpha = 1;
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:3 animations:^{
            self.image.alpha = 0;

        }];
    }];
    
    
}

@end
